﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC.Alarm
{
    public partial class AlarmManagement : UserControl
    {
        public event Action<List<string>> Event_AddAlarmInstrumentInfo;

        private ComboBox.SubmitUnit _CmbSubmitUnit;

        public AlarmManagement()
        {
            InitializeComponent();

            InitControls();

            btnSearch_Click(null, null);
        }


        private void InitControls()
        {
            PlugInCall("Main");
            InitComBoxGasType();

            pagInstrument.Event_SearchDataInfo += PagInstrument_Event_SearchDataInfo;
            pagInstrument.Event_UpdatePagingControl += btnSearch_Click;

            dgvAlarmDeatil.CellClick += DgvAlarmDeatil_CellClick;

            _CmbSubmitUnit = new ComboBox.SubmitUnit();
            _CmbSubmitUnit.Dock = DockStyle.Fill;
            pSubmitUnit.Controls.Add(_CmbSubmitUnit);
        }

        private void PagInstrument_Event_SearchDataInfo(int pageIndex, int pageSize)
        {
            int _StartIndex = (pageIndex * pageSize) + 1;
            int _SearchCount = pageSize;

            Dictionary<string, string> _LogisticList = GetLogisticList();

            List<Model.DB.tb_AlarminstrumentInfo> _ListInstrument = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentList(_StartIndex, _SearchCount, _LogisticList);
            dgvAlarmDeatil.DataSource = _ListInstrument;
        }

        private void DgvAlarmDeatil_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender != null && e.RowIndex >= 0)
            {
                if (dgvAlarmDeatil.Rows[e.RowIndex].Cells["Selected"].Value != null && (bool)dgvAlarmDeatil.Rows[e.RowIndex].Cells["Selected"].Value)
                    dgvAlarmDeatil.Rows[e.RowIndex].Cells["Selected"].Value = false;
                else
                    dgvAlarmDeatil.Rows[e.RowIndex].Cells["Selected"].Value = true;
            }
        }

        private void InitComBoxGasType()
        {
            Static.Methods.InitComBox(ref cmbGasType, Model.SystemEnum.ComBoxType.GasType);
        }

        /// <summary>
        /// 插件调用
        /// </summary>
        /// <param name="type">Main/Task </param>
        public void PlugInCall(string type)
        {
            switch (type)
            {
                case "Main":

                    pMain.Dock = DockStyle.Fill;
                    pMain.Visible = true;

                    pTask.Dock = DockStyle.None;
                    pTask.Visible = false;
                    Static.Methods.CreateDGVColumns(ref dgvAlarmDeatil, Model.SystemEnum.DGVType.AlarmDetailType);
                    break;

                case "Task":

                    pMain.Dock = DockStyle.None;
                    pMain.Visible = false;

                    pTask.Dock = DockStyle.Fill;
                    pTask.Visible = true;
                    Static.Methods.CreateDGVColumns(ref dgvAlarmDeatil, Model.SystemEnum.DGVType.AlarmSimpleType);
                    dgvAlarmDeatil.Columns["Delete"].Visible = false;
                    break;
            }
            btnSearch_Click(null, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int _DataCount = -1;

            Dictionary<string, string> _LogisticList;

            try
            {
                _LogisticList = GetLogisticList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "获取查询条件失败，提示信息：" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _CmbSubmitUnit.Focus();
                return;
            }

            _DataCount = new DataAccess.tb_AlarminstrumentInfo().GetAlarmInstrumentCount(_LogisticList);

            pagInstrument.IniEnabled(_DataCount);

            //dgvAlarmDeatil.DataSource = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentList();

        }

        private Dictionary<string, string> GetLogisticList()
        {
            Dictionary<string, string> _Result = new Dictionary<string, string>();


            //_Result.Add("SubmitUnit", checkSunmitUnit.Checked ? ((Model.DB.tb_SubmitUnit)cmbSubmitUnit.SelectedItem).Name : string.Empty);
            _Result.Add("SubmitUnit", checkSunmitUnit.Checked ? _CmbSubmitUnit.Text : string.Empty);

            _Result.Add("FactoryName", checkFactoryName.Checked ? txtFactoryNumber.Text : string.Empty);

            _Result.Add("Model", checkModel.Checked ? txtModel.Text : string.Empty);


            _Result.Add("GasType", checkGasType.Checked ? cmbGasType.SelectedValue.ToString() : string.Empty);

            return _Result;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<string> _List;

            bool _Select = false;
            _List = new List<string>();

            for (int i = 0; i < dgvAlarmDeatil.Rows.Count; i++)
            {
                if (dgvAlarmDeatil.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvAlarmDeatil.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _List.Add(dgvAlarmDeatil.Rows[i].Cells["FactoryNumber"].Value.ToString());
            }

            if (_List.Count > 0)
            {
                Event_AddAlarmInstrumentInfo?.Invoke(_List);
            }
            else
            {
                MessageBox.Show(this, "请选择报警器！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Forms.AlarmInstrumentFrom AlarmInstrumentFrom = new Forms.AlarmInstrumentFrom();
            if (AlarmInstrumentFrom.ShowDialog() == DialogResult.OK)
            {
                new DataAccess.tb_AlarminstrumentInfo().SaveCheckInstrument(AlarmInstrumentFrom.AlarminstrumentInfo);

                if (!string.IsNullOrEmpty(AlarmInstrumentFrom.AlarminstrumentInfo.ImageInfo))
                {
                    string _InstrumentId = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentIdForFactoryNumber(AlarmInstrumentFrom.AlarminstrumentInfo.FactoryNumber);
                    if (!string.IsNullOrEmpty(_InstrumentId))
                    {
                        Static.Methods.UpdateImage(Model.SystemEnum.ImageType.Alarminstrument, _InstrumentId, AlarmInstrumentFrom.AlarminstrumentInfo.ImageInfo);
                    }
                }

                btnSearch_Click(null, null);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvAlarmDeatil.Rows.Count; i++)
            {
                if (dgvAlarmDeatil.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvAlarmDeatil.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvAlarmDeatil.Rows[i].Cells["InstrumentId"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择报警器！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    //由简单到深删除
                    //tb_AlarminstrumentHisDeailData
                    new DataAccess.tb_AlarminstrumentHisDeailData().DeleteAlarminstrumentHisDeail(_IndexList);
                    //tb_AlarminstrumentHisData
                    new DataAccess.tb_AlarminstrumentHisData().DeleteAlarminstrumentHis(_IndexList);
                    //tb_AlarminstrumentInfo
                    new DataAccess.tb_AlarminstrumentInfo().DeleteAlarminstrumentInfo(_IndexList);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "删除报警器失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgvAlarmDeatil.SelectedRows.Count <= 0)
            {
                MessageBox.Show(this, "请选择报警器信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //if (new DataAccess.tb_TaskDetails().CheckIsExist(dgvAlarmDeatil.SelectedRows[0].Cells["FactoryNumber"].Value.ToString()))
            //{
            //    MessageBox.Show(this, "报警器已存在与任务列表中，无法进行修改！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return;
            //}

            Model.DB.tb_AlarminstrumentInfo _Temp = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentForInstrumentIdSingle(dgvAlarmDeatil.SelectedRows[0].Cells["InstrumentId"].Value.ToString());

            if (_Temp == null)
            {
                MessageBox.Show(this, "报警器下无相关信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Forms.AlarmInstrumentFrom _AlarmInstrumentFrom = new Forms.AlarmInstrumentFrom();
            _AlarmInstrumentFrom.ModifyInfo(_Temp);

            if (_AlarmInstrumentFrom.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    _AlarmInstrumentFrom.AlarminstrumentInfo.InstrumentId = _Temp.InstrumentId;
                    new DataAccess.tb_AlarminstrumentInfo().UpdateAlarminstrumentInfo(_AlarmInstrumentFrom.AlarminstrumentInfo);

                    if (!string.IsNullOrEmpty(_AlarmInstrumentFrom.AlarminstrumentInfo.ImageInfo))
                    {
                        Static.Methods.UpdateImage(Model.SystemEnum.ImageType.Alarminstrument, _AlarmInstrumentFrom.AlarminstrumentInfo.InstrumentId, _AlarmInstrumentFrom.AlarminstrumentInfo.ImageInfo);
                    }

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改报警器失败，错误信息：" + ex.ToString());
                }
            }
        }
    }
}
